﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class UserManageLogForm : Form
    {
        SqlConnection sqlCon = new SqlConnection(@"server=WWW-9F0EE1A0959\SQLEXPRESS;integrated security=SSPI;database=MONITOR_db");
        SqlDataAdapter sda = new SqlDataAdapter();
        
        public UserManageLogForm()
        {
            InitializeComponent();
        }

        private void UserManageLogForm_Load(object sender, EventArgs e)
        {
            DataTable da = new DataTable();
            string strSql = "select time, dbuserid, dbusername, oper, userid, username, state, detail from tb_usermanage";
            SqlCommand cmd = new SqlCommand(strSql, sqlCon);
            sda.SelectCommand = cmd;
            sda.Fill(da);
            BindingSource bindingSource1 = new BindingSource();
            bindingSource1.DataSource = da;

            this.dataGridView1.DataSource = bindingSource1;
            this.bindingNavigator1.BindingSource = bindingSource1;

            this.dataGridView1.Columns[0].HeaderText = "时间";
            this.dataGridView1.Columns[1].HeaderText = "操作员工号码";
            this.dataGridView1.Columns[2].HeaderText = "操作员工姓名";
            this.dataGridView1.Columns[3].HeaderText = "操作类型";
            this.dataGridView1.Columns[4].HeaderText = "变更员工号码";
            this.dataGridView1.Columns[5].HeaderText = "变更员工姓名";
            this.dataGridView1.Columns[6].HeaderText = "变更状态";
            this.dataGridView1.Columns[7].HeaderText = "详细信息";

          

            this.dateTimePicker1.Value = DateTime.Now;
            this.dateTimePicker2.Value = DateTime.Now;
            this.checkBox1.Checked = false;
           
            
        }

        private string buildsqlstring()
        {
            string strSql = "select time, dbuserid, dbusername, oper, userid, username, state, detail from tb_usermanage where itemid > 0 ";
            if(this.textBox1.Text != "")
            {
                strSql += "and dbuserid = " + this.textBox1.Text;
            }
            if(this.textBox2.Text != "")
            {
                strSql += " and " + @"dbusername = '" + this.textBox2.Text + @"'";
            }
            if(this.comboBox1.SelectedItem != null)
            {
                if(this.comboBox1.Items[this.comboBox1.SelectedIndex] != "全部")
                {
                     strSql += " and " + @"oper = '" + this.comboBox1.Items[this.comboBox1.SelectedIndex] + @"'";
                }
            }
            if (this.textBox3.Text != "")
            {
                strSql += "and userid = " + this.textBox3.Text;
            }
            if (this.textBox4.Text != "")
            {
                strSql += " and " + @"username = '" + this.textBox4.Text + @"'";
            }

            if(this.checkBox1.Checked)
            {
                DateTime starttime = this.dateTimePicker1.Value;
                DateTime endtime = this.dateTimePicker2.Value;

                if (starttime <= endtime)
                {
                    strSql += " and " + @"time >=  '" + starttime.ToShortDateString() + @"' and  " + @"time <=  '" + endtime.ToShortDateString() + @"'";
                }
                else
                {
                    MessageBox.Show("开始时间必须小于结束时间");
                    return null;
                }
            }
            MessageBox.Show(strSql);
            return strSql;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable da = new DataTable();
            string strSql = this.buildsqlstring();
            if (strSql == null)
            {
                return;
            }
            SqlCommand cmd = new SqlCommand(strSql, sqlCon);
            sda.SelectCommand = cmd;
            sda.Fill(da);
            BindingSource bindingSource1 = new BindingSource();
            bindingSource1.DataSource = da;
            this.dataGridView1.DataSource = bindingSource1;
            this.bindingNavigator1.BindingSource = bindingSource1;
        }
    }
}
